package com.quectel.business.common.util.sys;

import com.quectel.sys.module.sys.dto.SmallFileDto;
import com.quectel.sys.module.sys.service.SmallFileService;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.spring.SpringUtils;
import lombok.Getter;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * 小文件操作帮助类
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2022-09-20 13:31:00
 */
@Component
public class SmallFileUtils {

    private static String API_FILE_CACHE = "API_FILE_CACHE";

    @DubboReference
    @Getter
    private SmallFileService smallFileService;

    /**
     * 根据文件ID获取文件
     *
     * @param fileId
     * @return
     */
    public static SmallFileDto getByFileId(Long fileId) {

        SmallFileUtils smallFileUtils = SpringUtils.getBean(SmallFileUtils.class);

        return CacheKit.cacheToRedis(
                () -> smallFileUtils.getSmallFileService().selectCacheById(fileId),
                API_FILE_CACHE + fileId,
                10 * 60
        );
    }

}
